<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // const arr = ['a', 'b', 'c']
    /* const o = { age: 18 }
    arr.forEach(function (item, index, originArr) {
      console.log(item, index, originArr, this)
    }, o); */
    // Array.prototype.forEach = Array.prototype.forEach || function () { }

    /* Array.prototype.forEach2 = function (callback, _this = window) {
      // this => arr
      for (let i = 0; i < this.length; i++) {
        callback.call(_this, this[i], i, this)
      }
    }
    const o = { age: 18 }
    arr.forEach2(function (item, index, originArr) {
      console.log(item, index, originArr, this)
    }, o); */
    // forEach => 如何退出 => 抛错


    /* const arr = ['a', 'b', 'c']
    Array.prototype.map2 = function (callback) {
      const newArr = []
      for (let i = 0; i < this.length; i++) {
        newArr.push(callback(this[i], i, this))
      }
      return newArr
    }
    console.log(arr.map2((item, index, originArr) => '~' + item + '~')) */

    /* const arr = [7, 9, 5, 1, 11, 19];
    Array.prototype.sort2 = function (callback) {
      // 把【排序】好的结果返回出去
      for (let i = 0; i < this.length - 1; i++) {
        for (let j = 0; j < this.length - i - 1; j++) {
          if (callback(this[j], this[j + 1]) > 0) {
            ;[this[j], this[j + 1]] = [this[j + 1], this[j]]
          }
        }
      }
      return this
    }
    console.log(arr.sort2((n1, n2) => n1 - n2)) */

    /* let num1 = 8
    let num2 = 9
      ;[num1, num2] = [num2, num1]
    console.log(num1, num2) */

    const arr = [7, 9, 5, 1, 11, 19];
    Array.prototype.filter = function (callback) {
      const newArr = []
      for (let i = 0; i < this.length; i++) {
        if (callback(this[i])) {
          newArr.push(this[i])
        }
      }
      return newArr
    }
    console.log(arr.filter(item => item > 9))
  </script>
</body>

</html>